<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseName">
              <a-input :disabled="formDisabled" v-model="model.storehouseName" placeholder="请输入仓库名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="仓管员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseManager">
              <select-user-by-dep
                placeholder="请选择仓管员"
                :multi="true"
                :disabled="formDisabled"
                v-model="model.storehouseManager" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="仓库状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isUse">
              <j-dict-select-tag :disabled="formDisabled" placeholder="请选择仓库状态" v-model="model.isUse" dictCode="use_status"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="库位数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseLocationNum">
              <a-input-number :disabled="formDisabled" :max="99999999" :min="0"  v-model="model.storehouseLocationNum" placeholder="请输入库位数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="仓库地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseAddress">
              <a-input :disabled="formDisabled" v-model="model.storehouseAddress" placeholder="请输入仓库地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="仓库所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="department">
              <select-team-input :disabled="formDisabled" :tdlx="3" v-model="model.department" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="仓库简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseIntro">
              <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.storehouseIntro" rows="4" placeholder="请输入仓库简介" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouseRemark">
              <a-textarea :disabled="formDisabled" :max-length="500" v-model="model.storehouseRemark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'

  export default {
    name: 'ErpStorehouseBaseForm',
    components: {
      SelectUserByDep,
      SelectTeamInput
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          isUse: '1'
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        storehouseName: [
            { required: true, message: '请输入仓库名称!', trigger: 'blur'},
        ],
        storehouseManager: [
            { required: true, message: '请选择仓管员!', trigger: 'change'},
        ],
        storehouseLocationNum: [
          { required: false, message: '请输入库位数!', trigger: 'blur' },
          { pattern: /^[0-9]*[1-9][0-9]*$/, message: '请输入正整数!' },
        ],
        storehouseAddress: [
            { required: true, message: '请输入仓库地址!', trigger: 'blur'},
        ],
        department: [
            { required: true, message: '请选择仓库所属团队!', trigger: 'change'},
        ],
        storehouseIntro: [
            { required: true, message: '请输入仓库简介!', trigger: 'blur'},
        ],
        isUse: [
            { required: true, message: '请选择仓库状态!', trigger: 'blur'},
        ]
        },
        url: {
          add: "/api/erp/purchase/storehouse/erpStorehouseBase/add",
          edit: "/api/erp/purchase/storehouse/erpStorehouseBase/edit",
          queryById: "/api/erp/purchase/storehouse/erpStorehouseBase/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs["form"].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            console.log(this.model)
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch((error) => {
              that.$emit('unButtonDisable');
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$emit('unButtonDisable');
          }
        })
      },
    }
  }
</script>
